package com.xu.system.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xu.redis.service.RedisService;
import com.xu.system.constant.RedisConstants;
import com.xu.system.enums.WorkDateTypeEnum;
import com.xu.system.mapper.SysWorkTimeConfigDetailMapper;
import com.xu.system.model.entity.SysWorkTimeConfig;
import com.xu.system.model.entity.SysWorkTimeConfigDetail;
import com.xu.system.model.pojo.workTime.WorkTimeConfigBo;
import com.xu.system.service.db.ISysWorkTimeConfigDetailService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 工作时间配置明细表 服务实现类
 * </p>
 *
 * @author 许乾伟
 * @since 2024-12-25 10:04:28
 */
@Service
public class SysWorkTimeConfigDetailServiceImpl extends ServiceImpl<SysWorkTimeConfigDetailMapper, SysWorkTimeConfigDetail> implements ISysWorkTimeConfigDetailService {
    @Resource
    RedisService redisService;
    @Override
    public boolean saveWorkTimeConfigDetail(Long configId, LocalDate date, Integer dateType) {
        QueryWrapper<SysWorkTimeConfigDetail> wrapper = new QueryWrapper<>();
        wrapper.eq(SysWorkTimeConfigDetail.CONFIG_ID, configId);
        wrapper.eq(SysWorkTimeConfigDetail.DATE, date);
        SysWorkTimeConfigDetail detail =  getOne(wrapper);
        if (detail == null){
            detail = new SysWorkTimeConfigDetail();
            detail.setConfigId(configId);
            detail.setDateType(dateType);
            detail.setYear(date.getYear());
            detail.setMonth(date.getMonthValue());
            detail.setDay(date.getDayOfMonth());
            detail.setDate(date);
            detail.setDateType(dateType);
            save(detail);
        } else {
            detail.setDateType(dateType);
            detail.setYear(date.getYear());
            detail.setMonth(date.getMonthValue());
            detail.setDay(date.getDayOfMonth());
            detail.setDate(date);
            detail.setDateType(dateType);
            updateById(detail);
        }
        //更新缓存
        if (WorkDateTypeEnum.HOLIDAY.getCode().equals(dateType)){
            redisService.deleteObject(RedisConstants.HOLIDAY_KEY);
            redisService.setCacheList(RedisConstants.HOLIDAY_KEY, getHolidays(configId));
        } else {
            redisService.deleteObject(RedisConstants.WORKDAY_KEY);
            redisService.setCacheList(RedisConstants.WORKDAY_KEY, getWorkDays(configId));
        }
        return Boolean.TRUE;
    }

    @Override
    public boolean delWorkTimeConfigDetail(Long configId, LocalDate date) {
        QueryWrapper<SysWorkTimeConfigDetail> wrapper = new QueryWrapper<>();
        wrapper.eq(SysWorkTimeConfigDetail.CONFIG_ID, configId);
        wrapper.eq(SysWorkTimeConfigDetail.DATE,date);
        SysWorkTimeConfigDetail detail = getOne(wrapper);
        if (detail == null){
            return Boolean.TRUE;
        }
        removeById(detail.getId());
        //更新缓存
        if (WorkDateTypeEnum.HOLIDAY.getCode().equals(detail.getDateType())){
            redisService.deleteObject(RedisConstants.HOLIDAY_KEY);
            redisService.setCacheList(RedisConstants.HOLIDAY_KEY, getHolidays(configId));
        } else {
            redisService.deleteObject(RedisConstants.WORKDAY_KEY);
            redisService.setCacheList(RedisConstants.WORKDAY_KEY, getWorkDays(configId));
        }
        return Boolean.TRUE;
    }

    @Override
    public List<LocalDate> getWorkDays(Long configId) {
        if (redisService.hasKey(getRedisKey(RedisConstants.WORKDAY_KEY, configId))) {
            return redisService.getCacheList(RedisConstants.WORKDAY_KEY);
        }
        QueryWrapper<SysWorkTimeConfigDetail> wrapper = new QueryWrapper<>();
        wrapper.eq(SysWorkTimeConfigDetail.CONFIG_ID, configId);
        wrapper.eq(SysWorkTimeConfigDetail.DATE_TYPE, WorkDateTypeEnum.WORKDAY.getCode());
        List<SysWorkTimeConfigDetail> list = list(wrapper);
        List<LocalDate> workDays = list.stream().map(SysWorkTimeConfigDetail::getDate).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(workDays)) {
            redisService.setCacheList(getRedisKey(RedisConstants.WORKDAY_KEY, configId), workDays);
        }
        return workDays;
    }

    @Override
    public List<LocalDate> getHolidays(Long configId) {
        if (redisService.hasKey(getRedisKey(RedisConstants.HOLIDAY_KEY, configId))) {
            return redisService.getCacheList(RedisConstants.HOLIDAY_KEY);
        }
        QueryWrapper<SysWorkTimeConfigDetail> wrapper = new QueryWrapper<>();
        wrapper.eq(SysWorkTimeConfigDetail.CONFIG_ID, configId);
        wrapper.eq(SysWorkTimeConfigDetail.DATE_TYPE, WorkDateTypeEnum.HOLIDAY.getCode());
        List<SysWorkTimeConfigDetail> list = list(wrapper);
        List<LocalDate> holidays = list.stream().map(SysWorkTimeConfigDetail::getDate).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(holidays)) {
            redisService.setCacheList(getRedisKey(RedisConstants.HOLIDAY_KEY, configId), holidays);
        }
        return holidays;
    }

    /**
     * 组装redis key
     * @param key redis key
     * @param configId 配置id
     * @return
     */
    private String getRedisKey(String key,Long configId){
        return String.format("%s%s", key, configId);
    }
}
